package com.nku.mason.mail;

import java.awt.BorderLayout;  
import java.awt.GridLayout;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.io.IOException;  
import java.io.PrintWriter;  
import java.net.InetAddress;  
import java.net.Socket;  
import java.util.Scanner;  
  
import javax.swing.*;  
  
/* 
 * 发送邮件客户端 
 * 
 * 1. 创建与SMTP服务器连接的Socket对象。
 * 2. 当连接成功，SMTP服务器就会返回一个应答码为220的响应，表示服务就绪。
 * 3. 逐条按行发送HELLO，MAIL FROM, RCPT TO,DATA,QUIT命令
 *    等待接收SMTP服务器的响应。
 */  
public class MailSender extends JFrame {  

	private static final long serialVersionUID = 1L;
	
	private JTextField from;  
    private JTextField to;  
    private JTextField server;  
    private JTextArea message;  
    private JTextArea comm;  
    private Scanner in;  
    private PrintWriter out;  
  
    public MailSender() {  
        // 设置frame  
        setSize(500, 600);  
        setTitle("MailSender");  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
       
        this.setLocationRelativeTo(null);  
  
        // 往frame中添加组件  
        JPanel p1 = new JPanel(new GridLayout(3, 2));  
        p1.add(new JLabel("from:"));  
        from = new JTextField(20);  
        p1.add(from);  
  
        p1.add(new JLabel("to:"));  
        to = new JTextField(20);  
        p1.add(to);  
  
        p1.add(new JLabel("SMTP server:"));  
        server = new JTextField(20);  
        p1.add(server);  
        add(p1, BorderLayout.NORTH);  
  
        JPanel p2 = new JPanel(new GridLayout(2, 1));  
        message = new JTextArea();  
        p2.add(new JScrollPane(message));  
  
        comm = new JTextArea();  
        comm.setEditable(false);  
        p2.add(new JScrollPane(comm));  
        add(p2, BorderLayout.CENTER);  
  
        JPanel p3 = new JPanel();  
        JButton sendButton = new JButton("Send");  
        p3.add(sendButton);  
        add(p3, BorderLayout.SOUTH);  
  
        // 给发送按钮注册监听器  
        sendButton.addActionListener(new ActionListener() {  
            @Override  
            public void actionPerformed(ActionEvent e) {  
                comm.setText("");  
                sendMail(server.getText().trim(), from.getText().trim(), to  
                        .getText().trim(), message.getText());  
            }  
        });  
        
        setVisible(true);  
    }  
  
    // 发送邮件  
    public void sendMail(String server, String from, String to, String message) {  
        try {  
            Socket s = new Socket(server, 25);  
            try {  
                in = new Scanner(s.getInputStream());  
                out = new PrintWriter(s.getOutputStream(), true/* 表示自动刷新 */);  
                String hostName = InetAddress.getLocalHost().getHostName();  
  
                receive();  
  
                send("HELLO" + hostName);// 握手，成功返回220表示服务就绪  
                receive();  
  
                send("MAIL FROM:<" + from + ">");//发件人邮箱  
                receive();  
  
                send("RCPT TO:<" + to + ">");//收件人邮箱  
                receive();  
  
                send("DATA");//表示开始发邮件内容了  
                receive();  
  
                send(message);//发送邮件你内容  
                send(".");  
                receive();  
                send("QUIT");  
            } finally {  
                s.close();  
            }  
        } catch (IOException e) {  
            comm.append("Error: " + e);  
        }  
    }  
  
    // 发送  
    private void send(String s) {  
        comm.append(s + '\n');  
        out.print(s);  
        out.print("\r\n");  
        out.flush();  
    }  
  
    // 接收  
    private void receive() {  
        comm.append(in.nextLine() + '\n');  
    }  
  
    public static void main(String[] args) {  
        new MailSender();  
    }  
}  
